package me.zhengjie.modules.web.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.system.domain.CourseClassStudent;
import me.zhengjie.modules.web.domain.VerifyCodeRequest;
import me.zhengjie.modules.web.enums.VerifyCodeSceneEnum;
import me.zhengjie.modules.web.service.IVerifyCodeService;
import me.zhengjie.modules.web.service.mybatisplus.ICourseClassStudentPlusService;
import me.zhengjie.utils.AliMessageUtil;
import me.zhengjie.utils.RedisUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private AliMessageUtil aliMessageUtil;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private ICourseClassStudentPlusService courseClassStudentPlusService;
    @Override
    public boolean sendVerifyCode(VerifyCodeRequest req, VerifyCodeSceneEnum sceneEnum) {
        String mobile = req.getMobile();
        if (StringUtils.isBlank(mobile)){
            throw new WeChatException("手机号不可为空");
        }
        if (null == sceneEnum){
            throw new WeChatException("非法请求");
        }
        //签到需要验证码用户名和手机号
        if (sceneEnum == VerifyCodeSceneEnum.SignIn){
            checkSignIn(mobile,req.getName(),req.getCourseNo());
        }
        String key = mobile + sceneEnum.getScene();
        if (!redisUtils.setIfAbsent(key,mobile,60L, TimeUnit.SECONDS)) {
            throw new WeChatException("稍等一分钟后在请求");
        }
        String code = RandomUtil.randomNumbers(4);
        boolean result = aliMessageUtil.verifyCode(mobile, code);
        if (result){
            redisUtils.set(VerifyCodeSceneEnum.getRedisKey(mobile,sceneEnum),code,60, TimeUnit.SECONDS);
        }
        return result;
    }

    private void checkSignIn(String mobile, String name,String courseNo){
        if (StringUtils.isAnyBlank(mobile,name,courseNo)){
            return;
        }
        LambdaQueryWrapper<CourseClassStudent> studentQueryCriteria = new LambdaQueryWrapper<>();
        studentQueryCriteria.eq(CourseClassStudent::getCourseNo,courseNo);
        List<CourseClassStudent> studentDtoList = courseClassStudentPlusService.list(studentQueryCriteria);
        if (CollectionUtils.isEmpty(studentDtoList)) {
            throw new WeChatException("信息未维护，请联系工作人员");
        }
        studentDtoList = studentDtoList.stream().filter(s -> StringUtils.isNotBlank(s.getUserName()) && StringUtils.isNotBlank(s.getPhoneNo())).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(studentDtoList)) {
            throw new WeChatException("信息未维护，请联系工作人员");
        }
        boolean matchName = studentDtoList.parallelStream().anyMatch(s -> s.getUserName().equals(name));
        if (!matchName) {
            throw new WeChatException("用户名输入有误");
        }
        boolean matchMobile = studentDtoList.parallelStream().anyMatch(s -> s.getPhoneNo().equals(mobile));
        if (!matchMobile) {
            throw new WeChatException("电话输入有误");
        }
        CourseClassStudent studentDto = studentDtoList.parallelStream().filter(s -> s.getUserName().equals(name) && s.getPhoneNo().equals(mobile)).findFirst().orElse(null);
        if (Objects.isNull(studentDto)){
            throw new WeChatException("姓名/电话都匹配的记录不存在");
        }
    }
}
